package org.example;

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.DocumentObjectType;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.interfaces.ICompositeObject;
import com.spire.doc.interfaces.IDocumentObject;

import javax.imageio.ImageIO;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: Climbing-pit
 * @Date: 2022/4/15 21:25
 * @Description: Doc工具类
 */
public class DocUtil {

    /**
     * 本地Doc文件提取Txt
     * @param filePath  文件路径
     * @param savePath  提取文件保存路径
     * @param tempFileName 文件名
     * @throws IOException
     */
    public static void extractedText(String filePath, String savePath, long tempFileName) throws IOException {
        //加载Word文档
        Document doc = new Document();
        doc.loadFromFile(filePath);
        //获取文档中的文本保存为String
        String text=doc.getText();

        //将String写入Txt文件
        writeStringToTxt(text,savePath+tempFileName+SystemConstant.DOC_TXT_SUFFIX);
    }

    /**
     * 本地Doc文件转Html
     * @param filePath  文件路径
     * @param savePath  提取文件保存路径
     * @param tempFileName 文件名
     * @throws IOException
     */
    public static void toHtml(String filePath, String savePath, long tempFileName) throws IOException {
        //加载Word文档
        Document doc = new Document();
        doc.loadFromFile(filePath);

        //保存为HTML格式
        doc.saveToFile(savePath+tempFileName+ SystemConstant.DOC_HTML_SUFFIX,FileFormat.Html);

        extractImages(filePath, savePath);


        doc.dispose();
    }


    public static void writeStringToTxt(String content, String txtFileName) throws IOException {

        FileWriter fWriter= new FileWriter(txtFileName,true);
        try {
            fWriter.write(content);
        }catch(IOException ex){
            ex.printStackTrace();
        }finally{
            try{
                fWriter.flush();
                fWriter.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    /**
     * 本地Doc文件提取图片
     * @param filePath  文件路径
     * @param savePath  提取文件保存路径
     * @throws IOException
     */
    public static void extractImages(String filePath, String savePath) throws IOException {
        //加载Word文档
        Document document = new Document();
        document.loadFromFile(filePath);

        //创建Queue对象
        Queue nodes = new LinkedList();
        nodes.add(document);

        //创建List对象
        List images = new ArrayList();

        //遍历文档中的子对象
        while (nodes.size() > 0) {
            ICompositeObject node = (ICompositeObject) nodes.poll();
            for (int i = 0; i < node.getChildObjects().getCount(); i++) {
                IDocumentObject child = node.getChildObjects().get(i);
                if (child instanceof ICompositeObject) {
                    nodes.add(child);

                    //获取图片并添加到List
                    if (child.getDocumentObjectType() == DocumentObjectType.Picture) {
                        DocPicture picture = (DocPicture) child;
                        images.add(picture.getImage());
                    }
                }
            }
        }

        Integer size = images.size();
        //将图片保存为PNG格式文件
        for (int i = 0; i < size; i++) {
            File file = new File(String.format(savePath+"图片-%d.png", i));
            ImageIO.write((RenderedImage) images.get(i), "PNG", file);
        }

    }

    public static void main(String[] args) {

        long l = System.currentTimeMillis();
        String filePath = "D:\\files\\practice\\明日提升练习.docx";



        //创建保存路径
        String parentFilePath = filePath.substring(0, filePath.lastIndexOf(File.separatorChar)+1);
        String savePath = parentFilePath+SystemConstant.TEST_PAPER_SAVE_PATH+l+File.separator;
        File parent = new File(savePath);
        parent.mkdirs();

        try {
            DocUtil.extractedText(filePath, savePath, l);
            DocUtil.toHtml(filePath, savePath, l);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}



 class SystemConstant {
    public static final String DOC_HTML_SUFFIX= ".html";
    public static final String DOC_TXT_SUFFIX= "_ExtractedText.txt";
    public static final String TEST_PAPER_SAVE_PATH= "testPaper"+ File.separator;
    public static final String DOC_SAVE_PATH= "practice"+ File.separator;

}